package com.zhangxin.aiInterview.service.impl;

import com.zhangxin.aiInterview.model.entity.DwsResumeQualityScore1d;
import com.zhangxin.aiInterview.model.entity.DwsResumeQualityScoreTd;
import com.zhangxin.aiInterview.model.entity.Resume;
import com.zhangxin.aiInterview.model.vo.response.ResumeQualityScoreDailyResponse;
import com.zhangxin.aiInterview.model.vo.response.ResumeQualityScoreHistoryResponse;
import com.zhangxin.aiInterview.service.DwsResumeQualityScore1dService;
import com.zhangxin.aiInterview.service.DwsResumeQualityScoreTdService;
import com.zhangxin.aiInterview.service.EchartsService;
import com.zhangxin.aiInterview.service.ResumeService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Echarts图表数据服务实现类
 */
@Service
public class EchartsServiceImpl implements EchartsService {
    @Resource
    private DwsResumeQualityScore1dService dwsResumeQualityScore1dService;
    
    @Resource
    private DwsResumeQualityScoreTdService dwsResumeQualityScoreTdService;
    
    @Resource
    private ResumeService resumeService;
    
    @Override
    public List<ResumeQualityScoreDailyResponse> getResumeQualityScoreDaily(Long userId, String date) {
        List<DwsResumeQualityScore1d> scoreList;
        
        // 如果日期为空，使用前一天的日期
        if (date == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            date = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        }
        
        // 查询指定日期的简历评分记录
        scoreList = dwsResumeQualityScore1dService.lambdaQuery()
                .eq(DwsResumeQualityScore1d::getUserId, userId)
                .eq(DwsResumeQualityScore1d::getCountTIme, date)
                .list();
        
        if (scoreList == null || scoreList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取所有相关的简历ID
        List<Long> resumeIds = scoreList.stream()
                .map(DwsResumeQualityScore1d::getResumeId)
                .collect(Collectors.toList());
        
        // 批量查询简历信息
        List<Resume> resumeList = resumeService.lambdaQuery()
                .in(Resume::getId, resumeIds)
                .list();
        
        // 将简历信息映射到ID
        Map<Long, Resume> resumeMap = resumeList.stream()
                .collect(Collectors.toMap(Resume::getId, resume -> resume));
        
        // 构建响应对象
        List<ResumeQualityScoreDailyResponse> responseList = new ArrayList<>(scoreList.size());
        for (DwsResumeQualityScore1d score : scoreList) {
            ResumeQualityScoreDailyResponse response = new ResumeQualityScoreDailyResponse();
            // 复制基本字段
            BeanUtils.copyProperties(score, response);
            
            // 从简历信息中添加额外字段
            Resume resume = resumeMap.get(score.getResumeId());
            if (resume != null) {
                response.setResumeName(resume.getResumeName());
                response.setResumeUrl(resume.getResumeUrl());
                response.setCompany(resume.getCompany());
                response.setPosition(resume.getPosition());
            }
            
            responseList.add(response);
        }
        
        return responseList;
    }
    
    @Override
    public List<ResumeQualityScoreHistoryResponse> getResumeQualityScoreHistory(Long userId, String date) {
        List<DwsResumeQualityScoreTd> scoreList;
        
        // 根据是否有日期参数进行查询
        if (date != null) {
            scoreList = dwsResumeQualityScoreTdService.lambdaQuery()
                    .eq(DwsResumeQualityScoreTd::getUserId, userId)
                    .eq(DwsResumeQualityScoreTd::getCountTIme, date)
                    .list();
        } else {
            scoreList = dwsResumeQualityScoreTdService.lambdaQuery()
                    .eq(DwsResumeQualityScoreTd::getUserId, userId)
                    .list();
        }
        
        if (scoreList == null || scoreList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取所有相关的简历ID
        List<Long> resumeIds = scoreList.stream()
                .map(DwsResumeQualityScoreTd::getResumeId)
                .collect(Collectors.toList());
        
        // 批量查询简历信息
        List<Resume> resumeList = resumeService.lambdaQuery()
                .in(Resume::getId, resumeIds)
                .list();
        
        // 将简历信息映射到ID
        Map<Long, Resume> resumeMap = resumeList.stream()
                .collect(Collectors.toMap(Resume::getId, resume -> resume));
        
        // 构建响应对象
        List<ResumeQualityScoreHistoryResponse> responseList = new ArrayList<>(scoreList.size());
        for (DwsResumeQualityScoreTd score : scoreList) {
            ResumeQualityScoreHistoryResponse response = new ResumeQualityScoreHistoryResponse();
            // 复制基本字段
            BeanUtils.copyProperties(score, response);
            
            // 从简历信息中添加额外字段
            Resume resume = resumeMap.get(score.getResumeId());
            if (resume != null) {
                response.setResumeName(resume.getResumeName());
                response.setResumeUrl(resume.getResumeUrl());
                response.setCompany(resume.getCompany());
                response.setPosition(resume.getPosition());
            }
            
            responseList.add(response);
        }
        
        return responseList;
    }
}
